<template>
  <div id="beam-container">
    <div id="beam-param">

      <el-checkbox v-model="borderChecked" @click.native.stop border @change="doChange">边框</el-checkbox>

    </div>
    <div id="beam-canvas"></div>
  </div>
</template>

<script>
  import * as THREE from 'three'
  import OrbitControls from 'threejs-orbit-controls'

  export default {
    name: "Beam3D",
    data() {
      return {

        borderChecked: false,

        beam3D: null,
        lineSegments: null

      }
    },
    methods: {
      doChange(val) {
        console.log(val)
        this.borderChecked = val
        if (this.borderChecked) {
          this.addBorderLine()

        } else {
          this.beam3D.remove(this.lineSegments)
          this.lineSegments = null
        }
      },
      addBorderLine() {
        let cubeEdges = new THREE.EdgesGeometry(this.beam3D.geometry, 80);
        let edgesMtl = new THREE.LineBasicMaterial({color: 0xff0000});
        edgesMtl.depthTest = false
        this.lineSegments = new THREE.LineSegments(cubeEdges, edgesMtl);
        this.beam3D.add(this.lineSegments)

      }
    },
    mounted() {
      let scene = new THREE.Scene();
      scene.background = new THREE.Color(0xf0f0f0)
      var axes = new THREE.AxesHelper(100)
      scene.add(axes)
      var camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 10000);
      var controls = new OrbitControls(camera,document.getElementById('beam-canvas'));

      var raycaster = new THREE.Raycaster();
      var mouse = new THREE.Vector2();

      var renderer = new THREE.WebGLRenderer();


      renderer.setSize(4 * window.innerWidth / 5, window.innerHeight);
      document.getElementById('beam-canvas').appendChild(renderer.domElement)
      const beamRadius = 8;
      var beamShape = new THREE.Shape();
      beamShape.moveTo(0, 0)
      beamShape.lineTo(2 * beamRadius, 0);
      beamShape.moveTo(2 * beamRadius, -beamRadius)
      beamShape.absarc(2 * beamRadius, -beamRadius, beamRadius, Math.PI / 2, -Math.PI / 2, true)
      beamShape.moveTo(2 * beamRadius, -2 * beamRadius)
      beamShape.lineTo(0, -2 * beamRadius)
      beamShape.moveTo(0, -beamRadius)
      beamShape.absarc(0, -beamRadius, beamRadius, -Math.PI / 2, Math.PI / 2, true)
      beamShape.moveTo(0, 0)
      var extrudeSettings = {
        amount: 20,
        bevelEnabled: false,
        bevelSegments: 8,
        steps: 2,
        bevelSize: 1,
        bevelThickness: 1,
        depth: 400
      };
      var beamgeometry = new THREE.ExtrudeGeometry(beamShape, extrudeSettings);
      var tmaterial = new THREE.MeshNormalMaterial({
        vertexColors: THREE.VertexColors,
        flatShading: true
      });
      this.beam3D = new THREE.Mesh(beamgeometry, tmaterial)
      this.beam3D.position.set(10,40,20)
      scene.add(this.beam3D)

      camera.position.set(20, 10, 150);
      controls.update();


      var animate = function () {
        requestAnimationFrame(animate);
        // 通过摄像机和鼠标位置更新射线
        controls.update();
        renderer.render(scene, camera);
      };

      function onMouseMove(event) {
        event.preventDefault();
        mouse.x = ((event.clientX - renderer.domElement.getBoundingClientRect().left) / renderer.domElement.offsetWidth) * 2 - 1;
        mouse.y = -((event.clientY - renderer.domElement.getBoundingClientRect().top) / renderer.domElement.offsetHeight) * 2 + 1;

        raycaster.setFromCamera(mouse, camera);

        // 计算物体和射线的焦点
        let intersects = raycaster.intersectObjects(scene.children);
        for (var i = 0; i < intersects.length; i++) {
          intersects[i].object.material.color.set(0x6699ff);
        }
      }

     // window.addEventListener('click', onMouseMove, false);
      requestAnimationFrame(animate)
    }
  }
</script>

<style scoped>

</style>
